package com.baoming.web.action;
 
import java.util.List;

import com.baoming.container.ServiceContainer;
import com.baoming.domain.Properties;
import com.baoming.domain.Singup;
import com.baoming.domain.SingupEnter;
import com.baoming.service.IPropertiesService;
import com.baoming.service.ISingupEnterService;
import com.baoming.service.ISingupService;
import com.baoming.tool.XmlTool;
import com.baoming.util.LoginUtil;
import com.baoming.util.ServiceTool;

@SuppressWarnings("serial")
public class SingupExecutionAction extends BaseAction{

	private IPropertiesService propService = (IPropertiesService) ServiceContainer.getService(IPropertiesService.SERVICE) ; 
	private ISingupEnterService singService = (ISingupEnterService) ServiceContainer.getService(ISingupEnterService.SERVICE) ; 
	private ISingupService singupService = (ISingupService) ServiceContainer.getService(ISingupService.SERVICE);
	
	 
	/**
	 * 用户在线报名
	 */
	@Override
	public String execute() throws Exception {
		
		// 得到报名的Id
		String singupId = request.getParameter("singupId") ; 
		
		//报名id为空 则返回
		if(singupId == null || "".equals(singupId)){
			return ERROR ; 
		}
	 
		//验证码验证
		String code = request.getParameter("code") ;  
		
		if(code == null){
			return ERROR ; 
		}
		
		if(code == null || !LoginUtil.checkNumber(request, code.trim())){
			request.setAttribute("error", "3") ; 
			return ERROR ; 
		} 
		
		//得到报名信息
		Singup sing = singupService.selectSingUpById(singupId) ; 
		
		//验证标识码
		if(sing.getCode() != null && sing.getCode().length() > 0){
			String code2 = request.getParameter("code2") ; 
			if(!code2.equals(sing.getCode())){
				//不成功 返回标识码2
				request.setAttribute("error", 2) ; 
				return ERROR ; 
			}
		}
		 
		//查询出报名的所有属性值 
		List<Properties> props = propService.getPropertiesBySingupId(singupId) ;  
		//根据属性值提取页面的值 
		for(Properties p : props){
			//得到属性的名字
			 String propName = p.getName() ;
			 //得到页面的用户写的值
			 String propValue = request.getParameter(propName) ; 
			 //判断值是否为必须和用户写的值是否为空 则保存不成功
			 if(p.getRequested()==1&&(propValue == null||"".equals(propValue))){
				 return ERROR ; 
			 }
			 //加入页面值
			 p.setPageValue(propValue) ;  
		}
		String xml = XmlTool.createXml(props) ; 
		
		//保存用户添加的值
		SingupEnter enter = new SingupEnter() ; 
		enter.setAddTime(ServiceTool.getNowDate()) ; 
		enter.setSingupId(singupId) ; 
		enter.setXmlValue(xml) ; 
		
		//保存到数据库
		singService.saveSingupEnter(enter) ; 
		
		//页面放置cookie 
		ServiceTool.addCookie(response, "isSingUp", singupId, 3600*12*24) ; 
		
		//再一次报名比赛的id 
		request.setAttribute("singupId", singupId) ; 
		
		//进入主面的 了解我们的id 
		request.setAttribute("memberId", sing.getMemberId()) ;	
		//保存成功
		return SUCCESS ;
	}

	@Override
	public String add() {
		return null;
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public String manager() {
		return null;
	}

	@Override
	public String remove() {
		return null;
	}

	@Override
	public String removeGroup() {
		return null;
	}

	 
}
